<template>

  <el-dialog
      v-model="props.DepartmentPositionData.dialogVisible"
      title="新增"
      width="30%"
      :before-close="handleClose"
  >
    <div>岗位名称:<el-input style="width: 300px" v-model="props.DepartmentPositionData.fromData.position.positionName"/></div>
    <div>部门：<el-select v-model="props.DepartmentPositionData.fromData.valueDepartment"
                       @change="positionMothed"
                       class="m-2" placeholder="请选择" style="width: 300px"  :value ="props.DepartmentPositionData.departmentList">
      <el-option
          v-for="item in  props.DepartmentPositionData.departmentList"
          :key="item.departmentId"
          :label="item.departmentName"
          :value="item.departmentId"
      /></el-select></div>
    <div>
      上级： <TreeFather :DepartmentPosition="props.DepartmentPositionData"   ref="menuZ"  />
    </div>
    <div v-if="props.DepartmentPositionData.transition===0">
     分配默认权限： <el-select
          v-model="props.DepartmentPositionData.roleList"
          multiple
          filterable
          allow-create
          default-first-option
          :reserve-keyword="false"
      >
        <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.name"
        />
      </el-select>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="props.DepartmentPositionData.transition==0?insertMothed():updateMethod()">确认</el-button>
        <el-button type="primary" @click="props.DepartmentPositionData.dialogVisible = false">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {defineProps, onMounted, reactive, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import TreeFather from '../systemVue/TreeFather.vue'
import {useRouter} from "vue-router";
import axios from "axios";
const router=useRouter()

const menuZ =ref();
const props =defineProps ({
  DepartmentPositionData: {
    type: Object,
  },
});


const  fromData =reactive({

  roleList:[],

})

const defaultMenu = (menu)=>{
  if(menu==null){
    props.DepartmentPositionData.treeDefault.positionName='';
    props.DepartmentPositionData.treeDefault.positionId=1;
  }else{
    if(menu.fatherId==0){
      props.DepartmentPositionData.treeDefault.positionId=menu.positionId;
      props.DepartmentPositionData.treeDefault.positionName=menu.positionName;
    }else{
      props.DepartmentPositionData.treeDefault.positionId=menu.positionTable.positionId;
      props.DepartmentPositionData.treeDefault.positionName=menu.positionTable.positionName;
    }
    menuZ.value.gaoliang(menu.fatherId)

  }

}
const options = ref();
onMounted(()=>{
  //查询所有的角色
  axios.get("http://localhost:1688/sysUser/allRole").then((data)=>{
    console.log(data.data)
    options.value =data.data.data
  })
})
defineExpose({ defaultMenu })
const updateMethod =()=>{
  props.DepartmentPositionData.fromData.position.fatherId=menuZ.value.menuId()===1?0:menuZ.value.menuId()
  console.log(props.DepartmentPositionData.fromData.position)
  axios.put("http://localhost:1688/position_con/updatePositionTable",JSON.parse(JSON.stringify(props.DepartmentPositionData.fromData.position))).then(data=>{
    const val =data.data;
    console.log(val)
    if(val.code==403){
      ElMessage.error("权限不足")
    }else {
      console.log(data)
      router.go(0)
    }

  })

  const departmentPosition = {
    departmentId:props.DepartmentPositionData.fromData.valueDepartment,
    depposId:props.DepartmentPositionData.fromData.yuanDepartmentId,
  }
  console.log(departmentPosition)
  axios.put("http://localhost:1688/position_con/updateDuoDepartmentPosition",departmentPosition).then(data=>{
    const val =data.data;
    console.log(val)
    if(val.code==403){
      ElMessage.error("权限不足")
    }else {
      console.log(data)
      router.go(0)
    }

  })
  props.DepartmentPositionData.dialogVisible = false

}
const insertMothed =()=>{
  props.DepartmentPositionData.fromData.position.fatherId=menuZ.value.menuId()
  var from =new FormData();
  from.append("roleNameList",JSON.parse(JSON.stringify(props.DepartmentPositionData.roleList)))
  console.log(menuZ.value.menuId())
  const url = "http://localhost:1688/sysUser/arrayIdRole"
  axios.put(url,from).then(data=>{
    const val =data.data;
    console.log(val)
    if(val.code==403){
      ElMessage.error("权限不足")
    }else {
      console.log(data)
      fromData.roleList=data.data.data.join(",");
      const url = "http://localhost:1688/position_con/insertDepartmentPosition"
      var formData=new FormData();
      const position ={
        fatherId:props.DepartmentPositionData.fromData.position.fatherId,
        positionName:props.DepartmentPositionData.fromData.position.positionName

      }
      formData.append("position",JSON.stringify(position))
      formData.append("roleList",fromData.roleList)
      formData.append("departmentId",props.DepartmentPositionData.fromData.valueDepartment)
      axios.post(url,formData).then(data=>{
        const val =data.data;
        console.log(val)
        if(val.code==403){
          ElMessage.error("权限不足")
        }else {
          console.log(data)
          router.go(0);

        }

      })
    }


  })
  props.DepartmentPositionData.dialogVisible = false
}

</script>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
